using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MonsterManager : MonoBehaviour {
	
	[System.NonSerialized]
	public List<GameObject> MonsterList;
//	private List<GameObject> test;
	public RespawnInfo[] Respawn_Info;
	
	public GameObject[] MonsterObjects;
//	private float Begin_Time;
	void Awake()
	{
		if( null == Instance )
		{
			Instance = FindObjectOfType(typeof ( MonsterManager ) ) as MonsterManager;
			if( null != Instance )
			{
				Instance.InitMonsterManager();
			}
		}
		if( null == Instance )
		{
			GameObject obj = new GameObject("MonsterManager");
			Instance = obj.AddComponent(typeof ( MonsterManager ) ) as MonsterManager;
			Instance.InitMonsterManager();
		}
	}
	private static MonsterManager Instance = null;
	public static MonsterManager getInstance()
	{
		if( null == Instance )
		{
			Instance = FindObjectOfType(typeof ( MonsterManager ) ) as MonsterManager;
			if( null != Instance )
			{
				Instance.InitMonsterManager();
			}
		}
		if( null == Instance )
		{
			GameObject obj = new GameObject("MonsterManager");
			Instance = obj.AddComponent(typeof ( MonsterManager ) ) as MonsterManager;
			Instance.InitMonsterManager();
		}
		
		return Instance;
	}
	
	private void InitMonsterManager()
	{
		MonsterList = new List<GameObject>();
		
//		Begin_Time = Time.time;
		
		for(int InfoIndex = 0 ; InfoIndex < Respawn_Info.Length ; InfoIndex++)
		{
			for(int SquadIndex = 0 ; SquadIndex < Respawn_Info[InfoIndex].Squad.Length ; SquadIndex++)
			{
				GameObject object_ = GameObject.Instantiate( Respawn_Info[InfoIndex].Squad[ SquadIndex ]
															,Respawn_Info[InfoIndex].Position[ SquadIndex ]
															,Quaternion.identity) as GameObject ;
				MonsterList.Add( object_ );
			}
		}
		
	}
	void Update () {
		
		/////
		GameManager.getInstance().num = MonsterList.Count;
		/////
		for(int InfoIndex = 0 ; InfoIndex < Respawn_Info.Length ; InfoIndex++)
		{
			
			if( Respawn_Info[InfoIndex].time < Time.time - Respawn_Info[InfoIndex].Begin_time )
			{
				Respawn_Info[InfoIndex].Begin_time = Time.time;
				for(int SquadIndex = 0 ; SquadIndex < Respawn_Info[InfoIndex].Squad.Length ; SquadIndex++)
				{
					GameObject object_ = GameObject.Instantiate( Respawn_Info[InfoIndex].Squad[ SquadIndex ]
																,Respawn_Info[InfoIndex].Position[ SquadIndex ]
																,Quaternion.identity) as GameObject ;
					MonsterList.Add( object_ );
				}
			}
		}
		if( 0 < MonsterList.Count )
		{
			for(int ML_I = MonsterList.Count - 1 ; ML_I >= 0 ; ML_I--)
			{
				if( MonsterList[ML_I].GetComponent<Monster>().monster_data_.Max_HP <= 0.0f )
				{
					GameObject temp = MonsterList[ML_I];
					Vector3 test2 = temp.transform.position;
					MonsterList.RemoveAt( ML_I );
					
					DropManager.getInstance().CreateItem(test2,"temp");
					
					DestroyImmediate( temp );
				}
			}
		}
	}
}
